//
//  DYHeadView.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/2/3.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit
import SDCycleScrollView
import SwiftWeb
import SwiftDate

class DYHeadView: SYBaseView {
    
    var model: DYDanLanModel!
    
    var type: YuleType = .zhusu
    
    var cycleScrollView: SDCycleScrollView!
    
    let imgView: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "label_jiudian")
        v.isUserInteractionEnabled = true
        
        return v
    }()
    
    var bgView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(white: 0, alpha: 0.5)
        return v
    }()
    
    var jiudian: UIButton = {
        let v = UIButton()
        v.setTitle("酒店民宿", for: .normal)
        v.setTitleColor(UIColor(hex: 0x367FF6), for: .normal)
        v.titleLabel?.font = 18.autoFontSize()
        return v
    }()
    
    var jingqu: UIButton = {
        let v = UIButton()
        v.setTitle("旅游景区", for: .normal)
        v.setTitleColor(UIColor.white, for: .normal)
        v.titleLabel?.font = 18.autoFontSize()
        return v
    }()
    
    var meishi: UIButton = {
        let v = UIButton()
        v.setTitle("餐饮美食", for: .normal)
        v.setTitleColor(UIColor.white, for: .normal)
        v.titleLabel?.font = 18.autoFontSize()
        return v
    }()
    
    lazy var zhusuChoice:DYZhusuChoiceView = {
        let v = DYZhusuChoiceView(frame: CGRect.zero, currentVC: self.currentVC, type: .zhusu)
        return v
    }()
    
    lazy var jingquChoice:DYZhusuChoiceView = {
        let v = DYZhusuChoiceView(frame: CGRect.zero, currentVC: self.currentVC, type: .jingqu)
        v.isHidden = true
        return v
    }()
    
    lazy var meishiChoice:DYZhusuChoiceView = {
        let v = DYZhusuChoiceView(frame: CGRect.zero, currentVC: self.currentVC, type: .meishi)
        v.isHidden = true
        return v
    }()
    
    var chazhao: UIButton = {
        let v = UIButton()
        v.setTitle("查找", for: .normal)
        v.setTitleColor(UIColor.white, for: .normal)
        v.titleLabel?.font = 18.autoFontSize()
        v.backgroundColor = UIColor(hex: 0x367FF6)
        v.layer.cornerRadius = 8.autoWidth()
        return v
    }()
    
    func configure(_ model: DYDanLanModel) {
        
        self.model = model
        let picM: [DYLbLiModel] = model.lbLi ?? []
        cycleScrollView.imageURLStringsGroup = picM.map { IMGIP + ($0.strK ?? "") }
    }
    
    func hideAllChoiceView() {
        self.zhusuChoice.isHidden = true
        self.jingquChoice.isHidden = true
        self.meishiChoice.isHidden = true
    }
    
    init(frame: CGRect, currentVC: SYBaseViewController) {
        super.init(frame: frame)
        self.currentVC = currentVC
        self.initView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initView() {
        
        jiudian.addAction {[weak self] in
            if let `self` = self {
                self.jiudian.setTitleColor(UIColor(hex: 0x367FF6), for: .normal)
                self.jingqu.setTitleColor(UIColor.white, for: .normal)
                self.meishi.setTitleColor(UIColor.white, for: .normal)
                self.imgView.image = UIImage(named: "label_jiudian")
                self.clickedClosure?(0)
                self.hideAllChoiceView()
                self.zhusuChoice.isHidden = false
                self.type = .zhusu
            }
        }
        jingqu.addAction {[weak self] in
            if let `self` = self {
                self.jingqu.setTitleColor(UIColor(hex: 0x367FF6), for: .normal)
                self.jiudian.setTitleColor(UIColor.white, for: .normal)
                self.meishi.setTitleColor(UIColor.white, for: .normal)
                self.imgView.image = UIImage(named: "label_jingqu")
                self.clickedClosure?(1)
                self.hideAllChoiceView()
                self.jingquChoice.isHidden = false
                self.type = .jingqu
            }
        }
        meishi.addAction {[weak self] in
            if let `self` = self {
                self.meishi.setTitleColor(UIColor(hex: 0x367FF6), for: .normal)
                self.jingqu.setTitleColor(UIColor.white, for: .normal)
                self.jiudian.setTitleColor(UIColor.white, for: .normal)
                self.imgView.image = UIImage(named: "label_meishi")
                self.clickedClosure?(2)
                self.hideAllChoiceView()
                self.meishiChoice.isHidden = false
                self.type = .meishi
            }
        }
        chazhao.addAction {[weak self] in
            if let `self` = self {
                switch self.type {
                case .zhusu:
                    let vc = DYZSListViewController(params: self.zhusuChoice.zhusuParams)
                    self.currentVC.sy_push(vc)
                case .jingqu:
                    let vc = DYListViewController(params: self.zhusuChoice.jingquParams)
                    self.currentVC.sy_push(vc)
                case .meishi:
                    let vc = DYMSListViewController(params: self.zhusuChoice.meishiParams)
                    self.currentVC.sy_push(vc)
                
                }
                
            }
        }
        
        cycleView()
        addSubview(bgView)
        addSubview(imgView)
        addSubview(jiudian)
        addSubview(jingqu)
        addSubview(meishi)
        addSubview(self.zhusuChoice)
        addSubview(self.jingquChoice)
        addSubview(self.meishiChoice)
        addSubview(chazhao)
        bgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(150.autoWidth())
            make.left.right.equalToSuperview()
            make.height.equalTo(45.autoWidth())
        }
        imgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(142.autoWidth())
            make.left.right.equalToSuperview()
            make.height.equalTo(265.autoWidth())
        }
        jiudian.snp.makeConstraints { (make) in
            make.centerY.equalTo(bgView)
            make.left.equalTo(bgView)
            make.right.equalTo(jingqu.snp.left)
            make.height.equalTo(bgView)
            make.width.equalTo(jingqu)
        }
        jingqu.snp.makeConstraints { (make) in
            make.centerY.equalTo(bgView)
            make.right.equalTo(meishi.snp.left)
            make.height.equalTo(bgView)
            make.width.equalTo(meishi)
        }
        meishi.snp.makeConstraints { (make) in
            make.centerY.equalTo(bgView)
            make.right.equalTo(bgView)
            make.height.equalTo(bgView)
        }
        zhusuChoice.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(187.autoWidth())
            make.left.right.equalToSuperview()
            make.height.equalTo(160.autoWidth())
        }
        jingquChoice.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(187.autoWidth())
            make.left.right.equalToSuperview()
            make.height.equalTo(160.autoWidth())
        }
        meishiChoice.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(187.autoWidth())
            make.left.right.equalToSuperview()
            make.height.equalTo(160.autoWidth())
        }
        chazhao.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(350.autoWidth())
            make.centerX.equalToSuperview()
            make.left.equalToSuperview().offset(25.autoWidth())
            make.right.equalToSuperview().offset(-25.autoWidth())
            make.height.equalTo(44.autoWidth())
        }
    }
    
    
    func cycleView() {
        
        cycleScrollView = SDCycleScrollView(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH , height: 187.0.autoWidth()))
        cycleScrollView.delegate = self
        cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated
        cycleScrollView.autoScrollTimeInterval = 2// 自动滚动时间间隔
        cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        cycleScrollView.pageControlBottomOffset = -30
        cycleScrollView.pageDotColor = .gray
        cycleScrollView.currentPageDotColor = .blue
        cycleScrollView.isUserInteractionEnabled = true
        // 添加到view
        self.addSubview(cycleScrollView)
        
    }
    
}


extension DYHeadView: SDCycleScrollViewDelegate {
    
    func cycleScrollView(_ cycleScrollView: SDCycleScrollView!, didSelectItemAt index: Int) {
        print("选择了轮播图\(index)")
    }
}




